package io.itit.ecp.admin.provide.utils;


import io.itit.ecp.admin.common.enums.UnitEnums;
import io.itit.ecp.admin.provide.action.resp.statistics.StatisticsDataResp;
import io.itit.ecp.admin.provide.action.resp.statistics.StatisticsDiffResp;
import io.itit.grass.common.enums.IUnitEnums;
import io.itit.grass.common.utils.Y666Utils;

import java.math.BigDecimal;
import java.util.Map;

/**
 * @Author：Roy
 * @Description：
 * @Date： Created in 2022/6/20 10:01
 */
public class StatisticsUtils {

    public static StatisticsDataResp build(BigDecimal value) {
        return build(null, value);
    }

    public static StatisticsDataResp build(BigDecimal value, String unit) {
        return build(null, value, unit);
    }

    public static StatisticsDataResp build(String name, BigDecimal value) {
        return build(null, name, value, null);
    }

    public static StatisticsDataResp build(String name, BigDecimal value, Map<String, Object> extInfo) {
        return build(null, name, value, null, null, null, extInfo);
    }

    public static StatisticsDataResp build(String code, String name, BigDecimal value) {
        return build(code, name, value, null);
    }

    public static StatisticsDataResp build(String name, BigDecimal value, String unit) {
        return build(null, name, value, unit);
    }

    public static StatisticsDataResp build(String code, String name, BigDecimal value, String unit) {
        return build(code, name, value, null, unit, null);
    }

    public static StatisticsDataResp build(String name, BigDecimal value, BigDecimal compareValue, String unit, String compareUnit) {
        return build(null, name, value, compareValue, unit, compareUnit, null);
    }

    public static StatisticsDataResp build(String code, String name, BigDecimal value, BigDecimal compareValue, String unit, String compareUnit) {
        return build(code, name, value, compareValue, unit, compareUnit, null);
    }

    public static StatisticsDataResp build(String code, String name, BigDecimal value, BigDecimal compareValue, String unit, String compareUnit, Map<String, Object> extInfo) {
        return StatisticsDataResp.builder()
                .code(code).name(name)
                .value(Y666Utils.toStrScale2(value))
                .valueUnit(Y666Utils.toStrJoinUnit(value, 2, Y666Utils.calcUnit(value)))
                .compareValue(Y666Utils.toStrScale2(compareValue))
                .compareValueUnit(Y666Utils.toStrJoinUnit(compareValue, 2, Y666Utils.calcUnit(compareValue)))
                .extInfo(extInfo)
                .unit(unit)
                .compareUnit(compareUnit)
                .build();
    }

    public static StatisticsDataResp build(String title, BigDecimal curVal, BigDecimal preVal) {
        return StatisticsDataResp.builder()
                .name(title)
                .unit(UnitEnums.kgCO2.unit)
                .value(Y666Utils.toStrScale2(curVal))
                .valueUnit(Y666Utils.toStrJoinUnit(curVal, 2, Y666Utils.calcUnit(curVal)))
                .compareValue(Y666Utils.toStrScale2(preVal))
                .compareValueUnit(Y666Utils.toStrJoinUnit(preVal, 2, Y666Utils.calcUnit(preVal)))
                .compareRate(Y666Utils.calcRateJoinStr(curVal, preVal, 2, UnitEnums.per.unit))
                .compareOrder(Y666Utils.calcOrder(curVal, preVal))
                .compareColor(Y666Utils.calcColorB(curVal, preVal))
                .build();
    }

    public static StatisticsDataResp build(String title,UnitEnums enums, BigDecimal curVal, BigDecimal preVal) {
        return StatisticsDataResp.builder()
                .name(title)
                .unit(enums.unit)
                .value(Y666Utils.toStrScale2(curVal))
                .valueUnit(Y666Utils.toStrJoinUnit(curVal, 2, Y666Utils.calcUnit(curVal)))
                .compareValue(Y666Utils.toStrScale2(preVal))
                .compareValueUnit(Y666Utils.toStrJoinUnit(preVal, 2, Y666Utils.calcUnit(preVal)))
                .compareRate(Y666Utils.calcRateJoinStr(curVal, preVal, 2, UnitEnums.per.unit))
                .compareOrder(Y666Utils.calcOrder(curVal, preVal))
                .compareColor(Y666Utils.calcColorB(curVal, preVal))
                .build();
    }

    public static StatisticsDataResp build(String name, String value) {
        return build(null, name, value, null, null);
    }

    public static StatisticsDataResp build(String name, String value, Map<String, Object> extInfo) {
        return build(null, name, value, null, null, extInfo);
    }

    public static StatisticsDataResp build(String name, Integer value) {
        return build(null, name, Y666Utils.toStr(value), null);
    }

    public static StatisticsDataResp build(String code, String name, String value) {
        return build(code, name, value, null);
    }

    public static StatisticsDataResp build(String code, String name, String value, String unit) {
        return build(code, name, value, null, unit, null);
    }

    public static StatisticsDataResp build(String code, String name, String value, String compareValue, String unit) {
        return build(code, name, value, compareValue, unit, null);
    }

    public static StatisticsDataResp build(String code, String name, String value, String compareValue, String unit, Map<String, Object> extInfo) {
        return StatisticsDataResp.builder()
                .code(code).name(name)
                .value(value)
                .compareValue(compareValue)
                .extInfo(extInfo)
                .unit(unit).build();
    }

    public static StatisticsDiffResp calcBuild(BigDecimal preVal, BigDecimal curVal) {
        preVal = preVal == null ? BigDecimal.ZERO : preVal;
        curVal = curVal == null ? BigDecimal.ZERO : curVal;
        BigDecimal diffVal = curVal.subtract(preVal);

        StatisticsDiffResp diffResp = new StatisticsDiffResp();
        diffResp.setDiff(StatisticsDataResp.builder()
                .value(Y666Utils.toStrScale2(absDivide(diffVal, preVal)))
                .unit(UnitEnums.per.unit)
                .build());
        diffResp.setOrder(Y666Utils.calcOrder(curVal, preVal));
        diffResp.setColor(Y666Utils.calcColorB(curVal, preVal));
        return diffResp;
    }

    public static StatisticsDataResp calcBuild(String name, BigDecimal value) {
        return calcBuild(null, name, value, null, Y666Utils.calcUnit(value));
    }

    public static StatisticsDataResp calcBuild(String name, BigDecimal value, String unit) {
        return calcBuild(null, name, value, unit, Y666Utils.calcUnit(value));
    }

    public static StatisticsDataResp calcBuild(String name, BigDecimal value, String unit, Map<String, Object> extInfo) {
        return calcBuild(null, name, value, unit, Y666Utils.calcUnit(value), extInfo);
    }

    public static StatisticsDataResp calcBuild(String name, BigDecimal value, IUnitEnums.Unit unitEnum) {
        return calcBuild(null, name, value, null, unitEnum);
    }

    public static StatisticsDataResp calcBuild(String name, BigDecimal value, String unit, IUnitEnums.Unit unitEnum) {
        return calcBuild(null, name, value, unit, unitEnum);
    }

    public static StatisticsDataResp calcBuild(String code, String name, BigDecimal value, String unit) {
        return calcBuild(code, name, value, unit, Y666Utils.calcUnit(value), null);
    }

    public static StatisticsDataResp calcBuild(String code, String name, BigDecimal value, String unit, IUnitEnums.Unit unitEnum) {
        return calcBuild(code, name, value, unit, unitEnum, null);
    }

    public static StatisticsDataResp calcBuild(String code, String name, BigDecimal value, String unit, IUnitEnums.Unit unitEnum, Map<String, Object> extInfo) {
        return StatisticsDataResp.builder()
                .code(code).name(name)
                .value(Y666Utils.toStrScale2(value, unitEnum))
                .unit(Y666Utils.unit(unit, unitEnum))
                .extInfo(extInfo)
                .build();
    }

    public static StatisticsDataResp calcAmountBuild(String name, BigDecimal value, String unit) {
        return calcAmountBuild(null, name, value, unit, Y666Utils.calcUnit(value));
    }

    public static StatisticsDataResp calcAmountBuild(String name, BigDecimal value, String unit, IUnitEnums.Unit unitEnum) {
        return calcAmountBuild(null, name, value, unit, unitEnum);
    }

    public static StatisticsDataResp calcAmountBuild(String code, String name, BigDecimal value, String unit, IUnitEnums.Unit unitEnum) {
        return StatisticsDataResp.builder()
                .code(code).name(name)
                .value(Y666Utils.toStrScale2(value, unitEnum))
                .unit(Y666Utils.unitAmount(unit, unitEnum))
                .build();
    }

    public static StatisticsDataResp calcIntensityBuild(String name, BigDecimal value, String unit, Integer sceneType) {
        return calcIntensityBuild(null, name, value, unit, Y666Utils.calcUnit(value), sceneType);
    }

    public static StatisticsDataResp calcIntensityBuild(String code, String name, BigDecimal value, String unit, Integer sceneType) {
        return calcIntensityBuild(code, name, value, unit, Y666Utils.calcUnit(value), sceneType);
    }

    public static StatisticsDataResp calcIntensityBuild(String name, BigDecimal value, String unit, IUnitEnums.Unit unitEnum, Integer sceneType) {
        return calcIntensityBuild(null, name, value, unit, unitEnum, sceneType);
    }

    public static StatisticsDataResp calcIntensityBuild(String code, String name, BigDecimal value, String unit, IUnitEnums.Unit unitEnum, Integer sceneType) {
        return StatisticsDataResp.builder()
                .code(code).name(name)
                .value(Y666Utils.toStrScale2(value, unitEnum))
                .unit(Y666Utils.unitIntensity(unit, unitEnum, sceneType))
                .build();
    }

    /**
     * 取绝对值 计算百分比
     *
     * @param diff
     * @param divisor
     * @return
     */
    public static BigDecimal absDivide(BigDecimal diff, BigDecimal divisor) {
        if (BigDecimal.ZERO.compareTo(divisor) == 0 || BigDecimal.ZERO.compareTo(diff) == 0) {
            return diff;
        }
        if (BigDecimal.ZERO.compareTo(divisor) == 0) {
            return diff;
        }
        return diff.abs().divide(divisor, 4).multiply(new BigDecimal(100));
    }

    /**
     * 取商
     * 保留5位小数
     *
     * @param a
     * @param b
     * @return
     */
    public static BigDecimal divide(BigDecimal a, BigDecimal b) {
        a = a == null ? BigDecimal.ZERO : a;
        b = b == null ? BigDecimal.ZERO : b;

        if (BigDecimal.ZERO.compareTo(b) == 0) {
            return BigDecimal.ZERO;
        }

        return a.divide(b, 5, BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 取商
     * 保留5位小数
     *
     * @param a
     * @param b
     * @return
     */
    public static BigDecimal divide(Integer a, Integer b) {
        a = a == null ? 0 : a;
        b = b == null ? 0 : b;

        if (b == 0) {
            return BigDecimal.ZERO;
        }

        return divide(new BigDecimal(a), new BigDecimal(b));
    }

}

